package com.ruoyi.experiment.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.service.AnimalSpaciesService;
import com.ruoyi.experiment.service.IExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.experiment.domain.ExperimentDesign;
import com.ruoyi.experiment.service.IExperimentDesignService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验设计Controller
 * 
 * @author gw
 * @date 2025-01-08
 */
@RestController
@RequestMapping("/design")
public class ExperimentDesignController extends BaseController
{
    @Autowired
    private IExperimentDesignService experimentDesignService;

    @Autowired
    private IExperimentService experimentService;

    @Autowired
    private AnimalSpaciesService animalSpaciesService;

    /**
     * 查询实验设计列表
     */
    @RequiresPermissions("Experiment:design:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentDesign experimentDesign)
    {
        startPage();
        List<ExperimentDesign> list = experimentDesignService.selectExperimentDesignList(experimentDesign);
        for (ExperimentDesign design : list) {
            if (design.getExperimentId() != null) {
                Experiment experiment = experimentService.selectExperimentByExperimentId(design.getExperimentId());
                if (experiment != null) {
                    design.setExperimentName(experiment.getExperimentName());
                }
            }

            if (design.getSpeciesId() != null) {
                AjaxResult ajaxResult = animalSpaciesService.getInfo(design.getSpeciesId());
                if (ajaxResult != null ) {
                    Map map =(Map) ajaxResult.get("data");
                    design.setSpeciesName(map.get("speciesName").toString());
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出实验设计列表
     */
    @RequiresPermissions("Experiment:design:export")
    @Log(title = "实验设计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentDesign experimentDesign)
    {
        List<ExperimentDesign> list = experimentDesignService.selectExperimentDesignList(experimentDesign);
        ExcelUtil<ExperimentDesign> util = new ExcelUtil<ExperimentDesign>(ExperimentDesign.class);
        util.exportExcel(response, list, "实验设计数据");
    }

    /**
     * 获取实验设计详细信息
     */
    @RequiresPermissions("Experiment:design:query")
    @GetMapping(value = "/{designId}")
    public AjaxResult getInfo(@PathVariable("designId") Long designId)
    {
        return success(experimentDesignService.selectExperimentDesignByDesignId(designId));
    }

    /**
     * 新增实验设计
     */
    @RequiresPermissions("Experiment:design:add")
    @Log(title = "实验设计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentDesign experimentDesign)
    {
        return toAjax(experimentDesignService.insertExperimentDesign(experimentDesign));
    }

    /**
     * 修改实验设计
     */
    @RequiresPermissions("Experiment:design:edit")
    @Log(title = "实验设计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentDesign experimentDesign)
    {
        return toAjax(experimentDesignService.updateExperimentDesign(experimentDesign));
    }

    /**
     * 删除实验设计
     */
    @RequiresPermissions("Experiment:design:remove")
    @Log(title = "实验设计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{designIds}")
    public AjaxResult remove(@PathVariable Long[] designIds)
    {
        return toAjax(experimentDesignService.deleteExperimentDesignByDesignIds(designIds));
    }
}
